//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.bs.pub.action;

import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.itf.so.m30.self.ISaleOrderScriptMaintain;
import nc.vo.pub.BusinessException;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.vo.pubapp.pflow.PfUserObject;
import nc.vo.so.m30.entity.SaleOrderVO;
import nc.vo.so.m30.util.FeatureSelectUtil;

import java.util.ArrayList;
import java.util.List;

public class N_30_WRITE extends AbstractCompiler2 {
    public N_30_WRITE() {
    }

    public String getCodeRemark() {
        return "\n";
    }

    public Object runComClass(PfParameterVO vo) throws BusinessException {
        try {
            SaleOrderVO[] originBills = (SaleOrderVO[])vo.getCustomProperty("nc.bs.scmpub.pf.ORIGIN_VO_PARAMETER");
            super.m_tmpVo = vo;
            PfUserObject userObj = (PfUserObject)this.getUserObj();
            SaleOrderVO[] inCurVOs = (SaleOrderVO[])this.getVos();
            FeatureSelectUtil.fillCfgCodeVO(inCurVOs, userObj);
            SaleOrderVO[] updatevos = this.getUpdateVO(inCurVOs);
            ISaleOrderScriptMaintain maintainsrv = (ISaleOrderScriptMaintain)NCLocator.getInstance().lookup(ISaleOrderScriptMaintain.class);
            if (updatevos != null && updatevos.length > 0) {
                vo.m_preValueVo = inCurVOs[0];
                vo.m_preValueVos = inCurVOs;
                return maintainsrv.saleOrderUpdate(inCurVOs, userObj, originBills);
            }

            SaleOrderVO[] insertvos = this.getInsertVO(inCurVOs);
            if (insertvos != null && insertvos.length > 0) {
                return maintainsrv.saleOrderInsert(inCurVOs, userObj);
            }
        } catch (Exception var8) {
            ExceptionUtils.marsh(var8);
        }

        return null;
    }

    private SaleOrderVO[] getInsertVO(SaleOrderVO[] orderVos) {
        if (orderVos != null && orderVos.length != 0) {
            List<SaleOrderVO> newVos = new ArrayList();
            SaleOrderVO[] var3 = orderVos;
            int var4 = orderVos.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                SaleOrderVO vo = var3[var5];
                if (vo != null && vo.getParentVO() != null && (2 == vo.getParentVO().getStatus() && vo.getParentVO().getCsaleorderid() != null || vo.getParentVO().getCsaleorderid() == null)) {
                    newVos.add(vo);
                }
            }

            return (SaleOrderVO[])newVos.toArray(new SaleOrderVO[newVos.size()]);
        } else {
            return null;
        }
    }

    private SaleOrderVO[] getUpdateVO(SaleOrderVO[] orderVos) {
        if (orderVos != null && orderVos.length != 0) {
            List<SaleOrderVO> updateVos = new ArrayList();
            SaleOrderVO[] var3 = orderVos;
            int var4 = orderVos.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                SaleOrderVO vo = var3[var5];
                if (vo != null && vo.getParentVO() != null && (1 == vo.getParentVO().getStatus() || vo.getParentVO().getCsaleorderid() != null && 2 != vo.getParentVO().getStatus())) {
                    updateVos.add(vo);
                }
            }

            return (SaleOrderVO[])updateVos.toArray(new SaleOrderVO[updateVos.size()]);
        } else {
            return null;
        }
    }
}
